<template>
  <a-modal
    :open="open"
    width="640px"
    :title="`${isEdit ? '编辑检验组套' : '新增检验组套'}`"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" class="form-style-flex">
      <a-form-item label="组套名称" v-bind="validateInfos.setName" class="w-3/5">
        <a-input v-model:value="formState.setName" placeholder="请输入" @blur="getPinyin" />
      </a-form-item>
      <a-form-item label="助记码" class="w-2/5">
        <a-input v-model:value="formState.zjm" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="检验大类" class="w-3/5">
        <a-select v-model:value="formState.lisCategroyId" placeholder="请选择">
          <a-select-option v-for="(item, index) in inspectCategories" :key="index" :value="item.categroyCode">
            {{ item.categroyName }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="顺序号" class="w-2/5">
        <a-input-number v-model:value="formState.outorder" class="w-full" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="组套说明" class="w-full">
        <a-textarea v-model:value="formState.setMemo" :rows="5" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const formState = ref({
  setName: '',
  zjm: '',
  lisCategroyId: undefined,
  outorder: '',
  setMemo: ''
})
const rulesState = ref({
  setName: [{ required: true, message: '请输入组套名称' }]
})
const { isCompleted, resetFields, validate, validateInfos } = useForm(formState, rulesState)
const isEdit = ref(false)
watch(
  () => props.open,
  (val) => {
    if (val && Object.keys(props.rowData).length) {
      isEdit.value = true
      formState.value = Object.assign({}, { ...props.rowData })
    } else {
      isEdit.value = false
      formState.value = {
        setName: '',
        zjm: '',
        lisCategroyId: undefined,
        outorder: '',
        setMemo: ''
      }
    }
    if (val) getInspectCategories()
  }
)
const onCancel = () => {
  emit('update:open', false)
  resetFields()
}
const onOk = async () => {
  await validate()
  const { code, msg } = await post('/lis/LisPackage/doSaveLisAdviceSet', {
    ...formState.value
  })
  if (code === 0) {
    message.success('保存成功')
    emit('success')
    onCancel()
  } else {
    message.error(msg)
  }
}
const getPinyin = async () => {
  const { data, code } = await post('/common/util/getPinyin', { name: formState.value.setName })
  code === 0 && (formState.value.zjm = data)
}
const inspectCategories = ref([])

// 获取检验大类
const getInspectCategories = async () => {
  const res = await post('/lis/InstrumentType/getLisCategroyList')
  if (res.code !== 0) return message.error(res.msg)
  inspectCategories.value = res.data
}
</script>
